package routers

import (
	"log"
	"myserver/routers/admin"
	"runtime/debug"

	"github.com/gin-gonic/gin"
)

func Recover(c *gin.Context) {
	defer func() {
		if r := recover(); r != nil {
			//打印错误堆栈信息
			log.Printf("panic: %v\n", r)
			debug.PrintStack()
			//封装通用json返回
			c.JSON(200, gin.H{
				"code": "4444",
				"msg":  "服务器内部错误",
			})
		}
	}()
	//加载完 defer recover，继续后续接口调用
	c.Next()
}
func InitRouter() *gin.Engine {
	r := gin.New()
	r.Use(Recover)
	admin.RegisterUserRouters(r)
	admin.RegisterFileRouters(r)
	admin.RegisterGoodsRouters(r)
	admin.RegisterShopRouters(r)
	admin.RegisterClassificationRouters(r)
	return r
}
